package com.example.question.bt.backtrack1;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023-07-15
 * @Author Jonathan
 * @link {<a href="https://leetcode.cn/problems/permutations/">...</a>}
 */
public class Permutations {

    public static void main(String[] args) {
        Permutations code = new Permutations();
        List<List<Integer>> res = code.permute(new int[]{1, 2, 3});
        System.out.println(res);
    }

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        dfs(nums, -1, new ArrayList<>(), res);
        return res;
    }




    /**
     * 深度优先遍历
     */
    private void dfs(int[] nums, int index, List<Integer> path, List<List<Integer>> res) {
        // 结束递归的边界条件
        if (path.size() == nums.length) {
            return;
        }
        if (index != -1) {
            path.add(nums[index]);
        }

        // 加了之后 判断是不是满足条件了
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
        }

        // 使用for循环代替
        dfs(nums, 0, path, res);
        dfs(nums, 1, path, res);
        dfs(nums, 2, path, res);
        if (index != -1) {
            path.remove(path.size() - 1);
        }
    }
}
